#ifndef GLOBALQUEUE_H
#define GLOBALQUEUE_H

#include <QObject>
#include <QVector>
#include <QMutex>
#include <QWaitCondition>
#include "publicstruct.h"

class GlobalQueue : public QObject {
    Q_OBJECT
public:
    GlobalQueue(QObject *parent = nullptr) : QObject(parent) {}

    // 向队列中添加元素
    void enqueue(const DomainInfo &item) {
        QMutexLocker locker(&mutex);
        queue.append(item);
        condition.wakeOne(); // 唤醒等待的线程
    }

    // 从队列中取出元素
    DomainInfo dequeue() {
        QMutexLocker locker(&mutex);
        DomainInfo item;
        while (queue.isEmpty()) {
            //condition.wait(&mutex); // 如果队列为空，则等待
            item.Id =-1;
            return item;
        }
        item = queue.first();
        queue.removeFirst();
        return item;
    }

    int getQueueLen(){
        return queue.length();
       }
    void clearQueue()
     {
        queue.clear();
    }
signals:
    void itemAdded();

private:
    QVector<DomainInfo> queue;
    QMutex mutex;
    QWaitCondition condition;
};

#endif // GLOBALQUEUE_H
